package com.lab.controller.admin;

import com.lab.common.Result;
import com.lab.common.leavePageResult;
import com.lab.dto.LeaveApprovalDTO;
import com.lab.dto.LeaveRequestPageQueryDTO;
import com.lab.service.LeaveRequestService;
import com.lab.vo.LeaveRequestVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Api(tags = "魏子洋管理员接口")
@RestController
@RequestMapping("/admin")
@Slf4j
public class LeaveRequestController {
    @Autowired
     private LeaveRequestService adminService;

    /**
     * 管理员审批请假单接口
     * 需登录管理员账号（adminId从会话中获取）
     */
    @PostMapping("/approve")
    @ApiOperation("批假条")
    public Result approve( @Validated LeaveApprovalDTO leaveApprovalDTO ,
                          HttpSession session) {
        // 从会话中获取当前登录管理员ID（实际项目中建议用Token解析）
        Long adminId = (Long) session.getAttribute("adminId");
        adminId= (long) 1;
        if (adminId == null) {
          return Result.fail("请先登录管理员账号");
        }
        adminId= (long) 1;

        // 调用Service处理审批逻辑
        adminService.approve( leaveApprovalDTO, adminId);
        log.info("管理员审批成功：leaveId={}, 状态={}, 审批人={}");
        return Result.success("审批成功");
    }
    /**
     * 分页查询请假申请
     */
    /**
     * 分页查询请假申请
     */
    @GetMapping("/leave/page")
    @ApiOperation("分页查询请假申请")
    public Result<leavePageResult<LeaveRequestVO>> pageQuery(LeaveRequestPageQueryDTO queryDTO) {
        log.info("分页查询请假申请：{}", queryDTO);
        leavePageResult<LeaveRequestVO> pageResult = adminService.pageQuery(queryDTO);
        return Result.success(pageResult);
    }
}
